﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Web.App_Code;
using QLK.Database;
using QLK.Database.BLLs;

namespace QLK.Web
{
    public partial class CategoryDetails : System.Web.UI.Page
    {
        private Category Cates;
        private CategoryBLL CateBLL = new CategoryBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Load_Data();
        }

        private void Load_Data()
        {
            grvCategories.DataSource = CateBLL.Cate_GetAll();
            grvCategories.DataBind();
        }

        protected void btnAddMoreCate_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.Categories);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            CheckBox ck;
            for (int i = 0; i < grvCategories.Rows.Count; i++)
            {
                ck = (CheckBox)grvCategories.Rows[i].Cells[1].FindControl("ckDelete");

                if (ck.Checked == true)
                {
                    int CateID = int.Parse(ck.Attributes["_value"]);

                    if (CateBLL.Cate_DeleteByID(CateID))
                    {
                        Load_Data();
                        lblWarning.Text = "Delete successfully!";
                    }
                    else
                    {
                        Load_Data();
                        lblWarning.Text = "Delete failed!";
                    }
                }
            }
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
            int index = gvRow.RowIndex;
            ImageButton img = sender as ImageButton;

            Cates = new Category();
            Cates.CategoryName = grvCategories.Rows[index].Cells[3].Text.ToString();
            Response.Redirect(string.Format("{0}?{1}={2}&{3}={4}", PageUrls.Categories, Columns.CategoryID,
                img.CommandArgument, Columns.CategoryName, Cates.CategoryName));
        }
    }
}
